package com.lzy.business.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.tools.commons.BigDecimalUtil;
import com.lzy.product.apis.*;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by WIN7 on 2017/12/28.
 */
@Component
public class ProductCouponFacade {

    @Autowired
    ProductInfoServiceApi productInfoServiceApi;

    @Autowired
    ProductYieldRatioServiceApi productYieldRatioServiceApi;

    @Autowired
    ProductInfoProjectServiceApi productInfoProjectServiceApi;

    @Autowired
    ProjectServiceApi projectServiceApi;

    @Autowired
    OrderProductServiceApi orderProductServiceApi;

    /**
     * 获取列表
     * @param dto
     * @return
     */
    public PageInfo<ProductInfoDto> selectList(Pager pager, ProductInfoDto dto){
        Ensure.that(pager).isNotNull("07000001");
        Ensure.that(dto).isNotNull("07000001");
        dto.setType(BusinessType.ProductType.COUPON.getCode());
        //获取卡券产品列表
        PageInfo pageInfo = productInfoServiceApi.selectList(pager,dto);
        if(pageInfo != null && pageInfo.getList() != null){
            List<ProductInfoDto> productInfoDtoList = pageInfo.getList();
            for (ProductInfoDto piDto : productInfoDtoList) {
                if(piDto != null){
                    if(piDto.getId() != null){
                        //获取卡券类型（如“房车券 + 代金券”）
                        ProductInfoProjectDto pipDto = new ProductInfoProjectDto();
                        pipDto.setProductId(piDto.getId());
                        List<ProductInfoProjectDto> pipList = productInfoProjectServiceApi.query(pipDto);
                        if(pipList != null && pipList.size() > 0){
                            String couponName = "";
                            for(ProductInfoProjectDto proInfoProDto : pipList){
                                if(proInfoProDto != null && proInfoProDto.getName() != null){
                                    couponName = couponName + proInfoProDto.getName() + "+";
                                }
                            }
                            couponName = couponName.substring(0,couponName.length()-1);
                            if(!couponName.equals("")){
                                piDto.setCouponName(couponName);
                            }
                        }
                        //获取卡券套餐销售量
                        OrderProductDto orderProductDto = new OrderProductDto();
                        orderProductDto.setProductId(piDto.getId());
                        orderProductDto.setStatus(BusinessType.CouponOrderType.PAYING_SUCCESS.getCode());
                        orderProductDto.setType(BusinessType.OrderProductType.COUPONS.getCode());
                        Integer saleNumber = orderProductServiceApi.selectNumberByProductId(orderProductDto);
                        if(saleNumber != null){
                            piDto.setSaleNumber(saleNumber);
                        }
                    }
                }
            }
        }
        return pageInfo;
    }

    /**
     * 根据产品id获取返现收益比
     * @param productId
     * @return
     */
    public List<ProductYieldRatioDto> getCashBackRatioList(Long productId){
        Ensure.that(productId).isNotNull("07000001");
        List<ProductYieldRatioDto> productYieldRatioDtoList = productYieldRatioServiceApi.queryListByProductId(productId);
        if(productYieldRatioDtoList != null){
            return productYieldRatioDtoList;
        }
        return null;
    }

    /**
     * 转向新增页面
     * @return
     */
    public List<ProjectDto> toAdd(){
        ProjectDto projectDto = new ProjectDto();
        projectDto.setBusiType(BusinessType.ProjectBusiType.COUPON.getCode());
        projectDto.setStatus(BusinessType.StatusEnum.ENABLED.getCode());
        List<ProjectDto> projectDtoList = projectServiceApi.getList(projectDto);
        if(projectDtoList != null){
            return projectDtoList;
        }
        return null;
    }

    /**
     * 新增卡券产品
     * @param dto
     * @return
     */
    public Boolean insert(ProductInfoDto dto){
        Ensure.that(dto).isNotNull("07000001");
        //添加产品
        dto.setType(BusinessType.ProductType.COUPON.getCode());
        Long id = productInfoServiceApi.insert(dto);
        Ensure.that(id).isNotNull("07000002");
        //向产品与包含项关联表中添加单价和包含数量
        if(dto.getProjectList() != null){
            List<ProductInfoProjectDto> productInfoProjectDtoList = dto.getProjectList();
            for (ProductInfoProjectDto pipDto : productInfoProjectDtoList) {
                if(pipDto != null){
                    if(pipDto.getPrice() != null && pipDto.getContainNum() != null){
                        ProductInfoProjectDto infoProjectDto = new ProductInfoProjectDto();
                        infoProjectDto.setProductId(id);
                        infoProjectDto.setProjectId(pipDto.getProjectId());
                        infoProjectDto.setPrice(pipDto.getPrice());
                        infoProjectDto.setContainNum(pipDto.getContainNum());
                        infoProjectDto.setValidTime(pipDto.getValidTime());
                        Long l = productInfoProjectServiceApi.insert(infoProjectDto);
                        Ensure.that(l).isNotNull("01000016");
                    }
                }
            }
        }else if(dto.getProjectList() == null){
            return false;
        }
        //向卡券宝产品收益比率表中添加数据
        if(dto.getYieldRatioDtoList() != null){
            List<ProductYieldRatioDto> yieldRatioDtoList = dto.getYieldRatioDtoList();
            for (ProductYieldRatioDto pyrDto : yieldRatioDtoList) {
                if(pyrDto != null && pyrDto.getCashbackTime() != null && pyrDto.getCashbackRatio() != null){
                    ProductYieldRatioDto productYieldRatioDto = new ProductYieldRatioDto();
                    productYieldRatioDto.setCashbackTime(pyrDto.getCashbackTime());
                    productYieldRatioDto.setCashbackRatio(pyrDto.getCashbackRatio());
                    productYieldRatioDto.setProductId(id);
                    Long k = productYieldRatioServiceApi.insert(productYieldRatioDto);
                    Ensure.that(k).isNotNull("07000003");
                }
            }
        }else if(dto.getYieldRatioDtoList() == null){
            return false;
        }
        return true;
    }

    /**
     * 转向编辑页面
     * @return
     */
    public ProductInfoDto toEdit(Long productId){
        Ensure.that(productId).isNotNull("07000001");
        ProductInfoDto dto = productInfoServiceApi.findById(productId);
        if(dto != null && dto.getId() != null){
            ProductInfoProjectDto infoProjectDto = new ProductInfoProjectDto();
            infoProjectDto.setProductId(dto.getId());
            List<ProductInfoProjectDto> infoProjectDtoList = productInfoProjectServiceApi.query(infoProjectDto);
            if(infoProjectDtoList != null){
                dto.setProjectList(infoProjectDtoList);
            }
            List<ProductYieldRatioDto> yieldRatioDtoList = productYieldRatioServiceApi.queryListByProductId(dto.getId());
            if(yieldRatioDtoList != null){
                dto.setYieldRatioDtoList(yieldRatioDtoList);
            }
        }
        return dto;
    }

    /**
     * 编辑卡券产品
     * @param dto
     * @return
     */
    public Boolean update(ProductInfoDto dto){
        Ensure.that(dto).isNotNull("07000001");
        //卡券产品信息表可更新，也可不更新，即不用判断返回值
        productInfoServiceApi.update(dto);
        //删除产品与包含项关联关系
        ProductInfoProjectDto infoProjectDto = new ProductInfoProjectDto();
        infoProjectDto.setProductId(dto.getId());
        productInfoProjectServiceApi.deleteByProductId(infoProjectDto);
        //删除产品与返现比率表关联关系
        productYieldRatioServiceApi.delByProductId(dto.getId());
        if(dto.getProjectList() != null) {
            //更新卡券产品与包含项关联表
            List<ProductInfoProjectDto> pipDtoList = dto.getProjectList();
            for (ProductInfoProjectDto pipDto : pipDtoList) {
                if(pipDto != null){
                    ProductInfoProjectDto proInProDto = new ProductInfoProjectDto();
                    proInProDto.setProductId(dto.getId());
                    proInProDto.setProjectId(pipDto.getProjectId());
                    proInProDto.setContainNum(pipDto.getContainNum());
                    proInProDto.setPrice(pipDto.getPrice());
                    proInProDto.setValidTime(pipDto.getValidTime());
                    Long l = productInfoProjectServiceApi.insert(proInProDto);
                    Ensure.that(l).isNotNull("01000016");
                }
            }
        }
        if(dto.getYieldRatioDtoList() != null){
            List<ProductYieldRatioDto> yieldRatioDtoList = dto.getYieldRatioDtoList();
            for (ProductYieldRatioDto pyrDto:yieldRatioDtoList) {
                if(pyrDto != null){
                    ProductYieldRatioDto yieldRatioDto = new ProductYieldRatioDto();
                    yieldRatioDto.setProductId(dto.getId());
                    yieldRatioDto.setCashbackTime(pyrDto.getCashbackTime());
                    yieldRatioDto.setCashbackRatio(pyrDto.getCashbackRatio());
                    Long l = productYieldRatioServiceApi.insert(yieldRatioDto);
                    Ensure.that(l).isNotNull("07000003");
                }
            }
        }
        return true;
    }

    /**
     *  //获取卡券价格列表
     */
    public List<CouponPriceDto> getCouponPriceDtoList(){
        List<Integer> projectList = BusinessType.ProjectPriceType.projectList();
        if(projectList != null){
            List<CouponPriceDto> couponPriceDtoList = new ArrayList<>();
            for (Integer key:projectList) {
                List<Integer> valueList = BusinessType.ProjectPriceType.projectPriceList(key);
                if(valueList != null){
                    CouponPriceDto cpDto = new CouponPriceDto();
                    List<Integer> priceList = new ArrayList<>();
                    for (Integer code : valueList) {
                        Integer price = BusinessType.PriceType.getPrice(code);
                        if(price != null){
                            priceList.add(price);
                        }
                    }
                    cpDto.setCouponId(Long.valueOf(key));
                    cpDto.setName(BusinessType.ProjectType.getDesc(key));
                    cpDto.setPriceList(priceList);
                    couponPriceDtoList.add(cpDto);
                }
            }
            return couponPriceDtoList;
        }
        return null;
    }
}
